﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class EditPolicy : BasePage
    {
        private IPricePolicyBLL pricePolicyBLL = IocContainer.Instance.Resolve<IPricePolicyBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int policyId = 0;
                    int.TryParse(Request.QueryString["PolicyId"], out policyId);
                    if (policyId > 0)
                    {
                        PricePolicy policy = pricePolicyBLL.GetPolicyById(policyId);
                        this.txtName.Text = policy.PolicyName;
                        this.txtCode.Text = policy.PolicyCode;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                PricePolicy policy = new PricePolicy();
                policy.PolicyName = txtName.Text.Trim();
                policy.PolicyCode = txtCode.Text.Trim();

                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    pricePolicyBLL.Add(policy);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int policyId = 0;
                    int.TryParse(Request.QueryString["PolicyId"], out policyId);
                    policy.PolicyId = policyId;
                    pricePolicyBLL.Update(policy);
                    isOK = true;
                }

                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }
    }
}
